package com.aiju.widgit.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.app.config.ConfigManager;
import com.aiju.base.R;
import com.aiju.base.dialog.BaseDialog;
import com.aiju.bean.MPoints;
import com.aiju.common.PixValue;
import com.aiju.utils.EmojiFilter;
import com.aiju.utils.KeyBoardUtils;
import com.aiju.utils.L;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.PriceUtil;
import com.aiju.utils.T;
import com.aiju.widgit.layout.SettingItemTextView;

/**
 * @author: 佐为
 * @date: 2017-05-19 21:25
 * @version: V1.0
 * @Description: 描述
 */
public class EditPointDialog extends BaseDialog implements View.OnClickListener {
    private EditText contentEditText;
    private TextView leftTextView;
    private TextView rightTextView;

    private SettingItemTextView moneyTextView;
    private SettingItemTextView pointTextView;
    private TextView favorableTextView;

    private double favorable;

    public EditText getContentEditText() {
        return contentEditText;
    }

    public EditPointDialog(Context context) {
        super(context);
    }

    @Override
    protected int getDialogStyleId() {
        return R.style.dialog;
    }

    @Override
    protected View getView() {
        final View inflate = mLayoutInflater.inflate(R.layout.dialog_edit_point, null);
        moneyTextView = (SettingItemTextView) inflate.findViewById(R.id.set_money);
        pointTextView = (SettingItemTextView) inflate.findViewById(R.id.set_point);
        favorableTextView = (TextView) inflate.findViewById(R.id.set_favorable);

        leftTextView = (TextView) inflate.findViewById(R.id.tv_dialog_left);
        rightTextView = (TextView) inflate.findViewById(R.id.tv_dialog_right);
        contentEditText = (EditText) inflate.findViewById(R.id.ev_dialog_content);

        leftTextView.setOnClickListener(this);
        rightTextView.setOnClickListener(this);
        KeyBoardUtils.openKeybord(contentEditText, mContext);
        setdismissListeren(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                InputMethodManager inputMgr = (InputMethodManager) mContext
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMgr.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
            }
        });

        contentEditText.setFilters(new InputFilter[]{
                new EmojiFilter(), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String oriValue = dest.toString();
                StringBuffer sb = new StringBuffer(oriValue);
                sb.append(source);

                String newValue = sb.toString();
                Double aDouble = Double.valueOf(newValue);
                if (aDouble > favorable) {
                    T.ToastShort("优惠金额不可超过" + PriceUtil.formatDouble(favorable) + "元");
                    return "";
                }
                if (dest.toString().contains(".")) {
                    int index = dest.toString().indexOf(".");
                    int mlength = dest.toString().substring(index).length();
                    if (mlength == 3) {
                        return "";
                    }
                }
                return source;
            }
        }
        });
        return inflate;
    }

    public EditPointDialog initData(String point) {
        String money = PreferencesUtils.getString(mContext, "money");
        moneyTextView.setRightText("￥" + money);
        MPoints mPoints = ConfigManager.getInstance().getUser().getMPoints();
        if (mPoints != null) {
            Double a = Double.valueOf(money) * (Double.valueOf(mPoints.getUseType()) / 100);
            Double b = Double.valueOf(point) * 0.01;
            L.d("积分比率：" + a);
            L.d("积分兑换：" + b);
            favorable = Math.min(a, b);
            favorableTextView.setText("请在下方输入优惠金额，不超过" + PriceUtil.formatDouble(favorable) + "元");
            String favorablePrice = PriceUtil.formatDouble(favorable);
            contentEditText.setText(favorablePrice);
            contentEditText.setSelection(favorablePrice.length());
        }
        pointTextView.setRightText(String.valueOf(point));
        return this;
    }

    /**
     * 设置对话框LayoutParams
     *
     * @return
     */
    public EditPointDialog setLayoutParams() {
        WindowManager.LayoutParams lp = getDialogWindow().getAttributes();
        lp.width = PixValue.dp.valueOf(317);
        lp.height = PixValue.dp.valueOf(245);
        getDialogWindow().setGravity(Gravity.CENTER);
        getDialogWindow().setAttributes(lp);
        return this;
    }

    public EditPointDialog setContentText(String content) {
        this.contentEditText.setText(content);
        this.contentEditText.setSelection(content.length());
        return this;
    }

    public EditPointDialog setLeftText(String leftText) {
        this.leftTextView.setText(leftText);
        return this;
    }

    public EditPointDialog setRightText(String rightText) {
        this.rightTextView.setText(rightText);
        return this;
    }

    private OnEditClick mOnEditClick;

    public EditPointDialog setOnEditClick(OnEditClick onEditClick) {
        this.mOnEditClick = onEditClick;
        return this;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.tv_dialog_left) {
            if (mOnEditClick != null) {
                mOnEditClick.onCancel();
                dismiss();
            }
        } else if (i == R.id.tv_dialog_right) {
            if (mOnEditClick != null) {
                mOnEditClick.onSubmit(contentEditText.getText().toString());
                dismiss();
            }
        }
    }

    public interface OnEditClick {
        void onCancel();

        void onSubmit(String msg);
    }

}
